package org.linlinjava.litemall.admin.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.linlinjava.litemall.admin.annotation.LoginAdmin;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallTopic;
import org.linlinjava.litemall.db.domain.TodaySell;
import org.linlinjava.litemall.db.service.LitemallTopicService;
import org.linlinjava.litemall.db.service.TodaySellService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/admin/todaySell")
public class TodaySellController {
	
	@Autowired
    private TodaySellService todaySellService;
	
	@GetMapping("/list")
    public Object list(@LoginAdmin Integer adminId,@RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "limit", defaultValue = "10") Integer limit
                       ){
        if(adminId == null){
            return ResponseUtil.unlogin();
        }

        List<TodaySell> todaySellList = todaySellService.queryTodaySell(page,limit);
        Long total = todaySellService.queryCount();
        Map<String, Object> data = new HashMap<>();
        //data.put("todaySellList", todaySellList);
        data.put("total", total);
        data.put("items", todaySellList);
        return ResponseUtil.ok(data);
    }
	
	@PostMapping("/create")
    public Object create(@LoginAdmin Integer adminId, @RequestBody TodaySell todaySell){
		
        if(adminId == null){
            return ResponseUtil.unlogin();
        }
        //TodaySell todaySell = new TodaySell();
//        //todaySell.setId((Integer)todaySellMap.get("id"));
//        todaySell.setStartTime(startTime);
//        todaySell.setEndTime(startTime);
//        todaySell.setStartTime(startTime);
        todaySellService.insert(todaySell);
        return ResponseUtil.ok(todaySell);
    }

    @GetMapping("/read")
    public Object read(@LoginAdmin Integer adminId, Integer id){
        if(adminId == null){
            return ResponseUtil.unlogin();
        }

        if(id == null){
            return ResponseUtil.badArgument();
        }

        TodaySell todaySell = todaySellService.findById(id);
        Map<String, Object> data = new HashMap<>();
        data.put("todaySell", todaySell);
        return ResponseUtil.ok(data);
    }

    @PostMapping("/update")
    public Object update(@LoginAdmin Integer adminId, @RequestBody TodaySell todaySell){
        if(adminId == null){
            return ResponseUtil.unlogin();
        }
        todaySellService.updateTodaySell(todaySell);
        return ResponseUtil.ok(todaySell);
    }
}
